package itsos.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import org.springframework.web.servlet.ModelAndView;
import org.tmatesoft.svn.core.SVNDirEntry;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNNodeKind;

import itsos.dao.MainIssueDao;
import itsos.dao.RepositoryDao;
import itsos.model.MainIssue;
import itsos.model.Repository;
import itsos.service.DisplayRepositoryTree;
import itsos.service.RepositoryService;
import itsos.common.*;

public class RepositoryServiceImpl implements RepositoryService {
	
	private RepositoryDao repositoryDao;
	
	
	public void setRepositoryDao(RepositoryDao repositoryDao)
	{
		this.repositoryDao = repositoryDao;
	}
	
	public boolean insertRepository(HttpServletRequest request)
	{
		
		
		Repository repository = new Repository();
		repository.setRepositoryGroupCode(new Long(Long.parseLong(request.getParameter("repositoryGroupCode"))));
		repository.setRevision(request.getParameter("revision"));
		repository.setRevisionName(request.getParameter("revisionName"));
		repository.setInsertTime(new Timestamp(System.currentTimeMillis()));
		repository.setInsertUserID(ConstCode.SUPER_ADMIN_NUMBER);
		repository.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		repository.setUpdateUserID(repository.getInsertUserID());
		
		repositoryDao.insertRepository(repository);
		
		return true;
	}

	
	public List searchRepositoryTree2(HttpServletRequest request) {
		
		List<String> list = new ArrayList();
		
	//	String url = request.getParameter("url");
	//	String name = request.getParameter("name");
	//	String password = request.getParameter("password");
        String url = "http://svn.svnkit.com/repos/svnkit/";
        String name = "anymous";
        String password = "anymous";	
		String branch = request.getParameter("branch");
        
        url += branch;
		Iterator iterator;
		try {
				iterator = DisplayRepositoryTree.FullPac(url, name, password);
		
				while(iterator.hasNext())
				{
					SVNDirEntry entry = (SVNDirEntry)iterator.next();
					if(entry.getKind() == SVNNodeKind.DIR)
					{
						list.add(entry.getName());
					}
				}
			} catch (SVNException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			}
		
		
		return list;
	}
	
	
	
	
	public List searchRepositoryTree(HttpServletRequest request) {
		
		List<String> list = new ArrayList();
		
	//	String url = request.getParameter("url");
	//	String name = request.getParameter("name");
	//	String password = request.getParameter("password");
        String url = "http://svn.svnkit.com/repos/svnkit/";
        String name = "anymous";
        String password = "anymous";	
		
		
		
		Iterator iterator;
		try {
				iterator = DisplayRepositoryTree.FullPac(url, name, password);
		
				while(iterator.hasNext())
				{
					SVNDirEntry entry = (SVNDirEntry)iterator.next();
					if(entry.getKind() == SVNNodeKind.DIR)
					{
						list.add(entry.getName());
					}
				}
			} catch (SVNException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			}
		
		
		return list;
	}

	public List searchRepositoryBranch(HttpServletRequest request) {
		
	
		List<String> list = new ArrayList();
		try {
	//	String url = request.getParameter("url");
	//	String name = request.getParameter("name");
	//	String password = request.getParameter("password");
	//	String[] br = request.getParameterValues("branch");
			String url = "http://svn.svnkit.com/repos/svnkit/";
	        String name = "anymous";
	        String password = "anymous";		
	        String br[] = {"branches", "tags"};
		
		
		Iterator co;
		
			co = DisplayRepositoryTree.FullPac(url,name,password);
		
		Iterator[] iterator = new Iterator[br.length];
		for(int i=0;i<br.length;i++)
		{
			String clonurl = url + "/" + br[i];
			
			iterator[i] = DisplayRepositoryTree.FullPac(clonurl, name, password);
		}	
		while(co.hasNext())
		{
			
			
			SVNDirEntry entry = (SVNDirEntry)co.next();
			if(entry.getKind()==SVNNodeKind.DIR)
			{
				for(int i=0;i<br.length;i++)
				{
					int tp=1;
					list.add(tp,entry.getName());
					if(br[i].equals(entry.getName()))
					{
						while(iterator[i].hasNext())
						{
							SVNDirEntry vs = (SVNDirEntry)iterator[i].next();
							if(vs.getKind()==SVNNodeKind.DIR)
							{
								
									list.add(i,vs.getName());
								
							}
						}
					}
				}
			}
		}
		} catch (SVNException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return list;
	}
	
	
	
	public List viewRepository(HttpServletRequest request)
	{
		Repository repository = new Repository();
		List list;
		repository.setRepositoryGroupCode(new Long(Long.parseLong(request.getParameter("repositoryGroupCode"))));
		
		
		list = repositoryDao.getRepositoryByRepositoryGroup(repository.getRepositoryGroupCode());
		
		return list;
	}
	
	public List getRepository(long repositoryGroupCode)
	{
		List list;
		
		list = repositoryDao.getRepositoryByRepositoryGroup(repositoryGroupCode);
		
		return list;
	}
	
	public Repository getRepositoryByRevision(String revision)
	{
		return (Repository) repositoryDao.getRepositoryByRevision(revision);
			
	}

	public List getRepository() {
		List list = repositoryDao.getRepository();
		return list;
	}

	public Repository getReposioryByRepositoryID(HttpServletRequest request) {
		Repository repository = new Repository();
		
		repository = repositoryDao.getRepositoryByRepositoryID(new Long(Long.parseLong(request.getParameter("repositoryID"))));		
		
		return repository;
	}
	
	
}
